<template>
  <a-modal
    title="菜单管理"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          label="上级菜单"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select placeholder="上级菜单" :disabled="isDisabled" :allowClear="true"
                    v-decorator="[ 'pid', {rules: []} ]">
            <a-select-option v-for="item in parentMenus" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="菜单名称"
        >
          <a-input
            placeholder="菜单名称"
            v-decorator="[ 'name', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="权限唯一键"
        >
          <a-input
            placeholder="权限唯一键"
            v-decorator="[ 'keySign', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <div v-show="form.getFieldValue('pid') != undefined && form.getFieldValue('pid') != ''">
          <a-divider/>
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="赋予权限"
          >
            <a-select mode="multiple" placeholder="菜单权限"
                      v-decorator="[ 'actions', {rules: [], initialValue: ['add', 'query', 'edit', 'delete', 'other']} ]">
              <a-select-option key="add">新增</a-select-option>
              <a-select-option key="query">查询</a-select-option>
              <a-select-option key="edit">修改</a-select-option>
              <a-select-option key="delete">删除</a-select-option>
              <a-select-option key="other">特殊</a-select-option>
            </a-select>
          </a-form-item>
        </div>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate, selectParentMenu } from '@/api/system/menu'

  export default {
    data () {
      return {
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        isDisabled: false,
        parentMenus: [],

        form: this.$form.createForm(this)
      }
    },

    methods: {
      add () {
        this.visible = true
        selectParentMenu().then(res => {
          this.parentMenus = res.result
        })
      },
      edit (record) {
        this.id = record.id
        selectParentMenu().then(res => {
          this.parentMenus = res.result
        })
        this.visible = true
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['pid', 'name', 'keySign', 'actions'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.isDisabled = false
        this.form.resetFields()
      }
    }
  }
</script>
